#!/usr/bin/env ruby

require 'optparse'

module Opal
  class BuilderOptions < OptionParser
    def initialize
      @options = {}

      super do |opts|
        opts.banner = 'Usage: opal-build [options] -- [libname]'

        opts.separator ''
        opts.separator 'Options:'

        opts.on("-h", "--help", "Show this message") do
          puts opts
          exit
        end

        opts.on('-g', '--gem GEM_NAME', String,
                'Adds the specified GEM_NAME lib dir to Opal\'s load path.',
                'E.g.: opal-build --gem erubis -- erubis`',
                'Will build erubis.rb from the regular "erubis" gem') do |g|
          options[:gems] ||= []
          options[:gems] << g
        end

        opts.on('-s', '--stub STUB', String) do |stub|
          options[:stubs] ||= []
          options[:stubs] << stub
        end

        opts.on('-r', '--require LIBRARY', String,
                'Require the library before executing your script',
                'E.g.: opal-build --require opal-browser browser`',
                'Will build browser.rb from the Opal gem opal-browser') do |library|
          options[:requires] ||= []
          options[:requires] << library
        end

        opts.on('-o', '--output FILE', String,
                'Write the built lib to a file (defaults to STDOUT)') do |file|
          options[:output] = file
        end
      end
    end

    attr_reader :options
  end
end

option_parser = Opal::BuilderOptions.new
option_parser.parse!

if ARGV.empty?
  puts option_parser
else
  require 'opal'
  options = option_parser.options

  if options[:gems]
    require 'rubygems'
    options[:gems].each { |gem_name| Opal.use_gem gem_name }
  end

  if options[:requires]
    options[:requires].each { |required_lib| require required_lib }
  end

  lib_name = ARGV.first

  output = options[:output] ? File.open(options[:output], 'w') : $stdout
  output.puts Opal::Builder.build(lib_name.dup)
end


